<?php


namespace app\home\userBusiness;


use app\home\model\PygUser;
use think\Exception;
use think\facade\Cache;
use think\facade\Session;

class UserBusiness
{

    /**
     * 注册手机验证码的逻辑封装
     * @param $phone
     * @return bool|int
     * @throws Exception
     */
    public static function sendCaptcha($phone){
        //设置短信验证码的四位随机数
        $code = rand(1000,9999);
        //设置发送短信的信息
        $content = "[品优购]你正在注册品优购品台，验证码为{$code},请妥善保存,请勿泄露";
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $smsapi = "http://api.smsbao.com/";
        $user = ""; //短信平台帐号
        $pass = md5(""); //短信平台密码
        $content="{$content}";//要发送的短信内容
        $phone = "$phone";//要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);


        $time_key = 'reg_'.$phone;
        $code_key = 'reg_code'.$phone;
        $count_key = 'reg_count'.$phone;
        //第二次发送的时候
        //先获取上次发送的缓存
        $time =Cache::get($time_key);
        //设定一个60s时间
        $between = 60 - (time() - $time);
        //判断是否存在、如果存在并且时间大于0时、返回一个错误
        if (!empty($time) && $between > 0){
            throw new Exception("短信已发送成功、请{$between}后重试");
        }

        #一个手机号一天只能发送3次
        $count = Cache::get($code_key);
        if (!empty($count) && $count > 3){
            abort(2003,'今日发送超限、请明日再试');
        }
        //开启调试模式
        if (env('APP_DEBUG')){
            //调试模式
            $result = 0 ;
        } else{
            $result =file_get_contents($sendurl) ;
        }
        //判断是否等于0、不等于0是捕获异常
        if ($result != 0){
            throw new Exception('发送失败');
        }

        //第一次发送的时间
        Cache::set('reg_'.$phone,time(),60);
        #存储验证码，存入Cache缓存中 过期时间为300秒
        $cache =Cache::set('reg_a'.$phone,$code,300);
        //判断手机号是否注册过、如果没有注册过则存个一、否则自增一
        empty($cache) ?
            Cache::set($code_key,1,strtotime('23:59:59') - time())
             : Cache::inc($code_key);

        return env('APP_DEBUG')?$code:true;
    }



    /**
     * 第三方注册的逻辑封装
     * @param $user
     * @param $get_id
     * @param string $type
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function qq($user,$get_id,$type='qq'){
       $res = PygUser::where('openid',$get_id)->find();
       if (!$res){
           $res_id = PygUser::create([
                'username' => $user['nickname'],
               'nickname' => $user['nickname'],
               'phone' => '',
               'password' => password('123456'),
               'figure_url' => $user['figureurl_1'],
               'open_type' => $type,
               'openid' => $get_id,
           ]);
           $id = $res_id->id;
       }else{
           $id = $res->id;
       }
       Session::set('name',['data'=>$res['nickname'],'id'=>$id]);
       return true;
    }
}